package cmw.com.beijingnew.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;

import cmw.com.beijingnew.R;
import cmw.com.utils.StringHandler;

public class NewsDetailActivity extends Activity {

    private ImageView ib_back;
    private ImageView ib_fontsize;
    private ImageView ib_share;
    private WebView webview;
    private ProgressBar pb_status;
    private WebSettings settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        findViews();
        showTitleMenus();
        addEvents();
        loadData();
    }

    private void loadData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String time = intent.getStringExtra("time");
        String src = intent.getStringExtra("src");
        String pic = intent.getStringExtra("pic");
        String url = intent.getStringExtra("url");
        String webUrl = intent.getStringExtra("weburl");
        String content = intent.getStringExtra("content");
        if(!StringHandler.isValidStr(url)) url = webUrl;

        settings = webview.getSettings();
        settings.setDefaultTextEncodingName("UTF-8");
        //设置支持JS
        settings.setJavaScriptEnabled(true);
        //设置双击变大变小
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //增加缩放按钮
        settings.setBuiltInZoomControls(true);
        //当点击页面超链接时不让当前网页跳转到系统的浏览器中
        webview.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pb_status.setVisibility(View.GONE);
            }
        });

        StringBuilder sbHtml = new StringBuilder();
        sbHtml.append(" <!DOCTYPE html>")
                .append("<html>")
                .append("<head> ")
                .append(" <meta charset='utf-8'>")
                .append("<meta name='viewport'" +
                 " content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0' />")
                .append("</head>")
                .append("<body>");

        sbHtml.append("<a href='"+url+"' style='color:black;'>")
                .append("<h3 style='text-align:center;'>").append(title).append("</h3>")
                .append("</a>")
                .append(src)
                .append("&nbsp;&nbsp;&nbsp;&nbsp;")
                .append(time);
        if(StringHandler.isValidStr(pic)){
            sbHtml.append("<br/><img src='"+pic
                    +"' style='text-align:center;border:0px;width:300px;height:150px;margin:10px;'/><br/>");
        }
        sbHtml.append(content);
        sbHtml.append("</body>")
                .append("</html>");
        webview.loadData(sbHtml.toString(), "text/html;charset=UTF-8", null);
    }

    private void findViews(){
        ib_back = (ImageView) findViewById(R.id.ib_back);
        ib_fontsize = (ImageView) findViewById(R.id.ib_fontsize);
        ib_share = (ImageView) findViewById(R.id.ib_share);
        webview = (WebView) findViewById(R.id.webview);
        pb_status = (ProgressBar) findViewById(R.id.pb_status);
    }

    private void showTitleMenus(){
        ib_back.setVisibility(View.VISIBLE);
        ib_fontsize.setVisibility(View.VISIBLE);
        ib_share.setVisibility(View.VISIBLE);
    }

    private void addEvents(){
        ib_back.setOnClickListener(new MyOnClickListener(ib_back));
        ib_fontsize.setOnClickListener(new MyOnClickListener(ib_fontsize));
        ib_share.setOnClickListener(new MyOnClickListener(ib_share));
    }

    class MyOnClickListener implements View.OnClickListener {
        View view = null;

        public MyOnClickListener(View view) {
            this.view = view;
        }

        @Override
        public void onClick(View _view) {
           if(view == ib_back){
               finish();
           }else if(view == ib_fontsize){
               showChangeTextSizeDialog();
           }else if(view == ib_share){
               Toast.makeText(NewsDetailActivity.this,"点击分享",Toast.LENGTH_SHORT).show();
           }
        }
    }

    int tempSize = 2;
    int realSize = tempSize;
    /**
     * 弹出设置字体大小的对话框
     */
    private void showChangeTextSizeDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("设置文字大小");
        String[] items = {"超大字体","大字体","正常字体","小字体","超小字体"};
        builder.setSingleChoiceItems(items, realSize, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                tempSize = i;
            }
        });
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                realSize = tempSize;
                changeTextSize(tempSize);
            }
        });
        builder.show();
    }

    /**
     *  改变WebView文字大小
     * @param tempSize
     */
    private void changeTextSize(int tempSize) {
        switch (tempSize){
            case 0 :    //超大字体
                settings.setTextZoom(200);
                break;
            case 1 :    //大字体
                settings.setTextZoom(150);
                break;
            case 2 :    //正常字体
                settings.setTextZoom(100);
                break;
            case 3 :    //小字体
                settings.setTextZoom(75);
                break;
            case 4 :    //超小字体
                settings.setTextZoom(50);
                break;
        }
    }
}
